package com.liuxing.javaSpringBoot.modules.common.service.impl;

import com.liuxing.javaSpringBoot.modules.common.dao.ExceptionLogDao;
import com.liuxing.javaSpringBoot.modules.common.entity.ExceptionLog;
import com.liuxing.javaSpringBoot.modules.common.entity.Result;
import com.liuxing.javaSpringBoot.modules.common.service.ExceptionLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @File ExceptionLogServiceImpl.java
 * @Auth liuxing
 * @Date 2021/8/25 09:19
 * @Email liuxing997@foxmail.com
 */
@Service
public class ExceptionLogServiceImpl implements ExceptionLogService {

    @Autowired
    private ExceptionLogDao exceptionLogDao;

    //插入异常日志
    @Override
    @Transactional
    public Result<ExceptionLog> insertExceptionLog(ExceptionLog exceptionLog) {
        exceptionLog.setCreateDate(LocalDateTime.now());
        List<ExceptionLog> exceptionLogs =
                Optional.ofNullable(
                        exceptionLogDao.getExceptionLogsByParam(
                                exceptionLog.getPath(),
                                exceptionLog.getMethodName(),
                                exceptionLog.getExceptionType()
                        )
                ).orElse(Collections.emptyList());
        if (exceptionLogs.size()==0){
            exceptionLogDao.insertExceptionLog(exceptionLog);
        }
        return new Result<>(Result.ResultCode.SUCCESS.code,
                "insert success");
    }
}
